package com.platform.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.platform.dao.ApiPostsMapper;
import com.platform.entity.PostsVo;

/**
 * 春芽社区故事Service实现类
 *
 * @author czj
 * @email 729618941@qq.com
 * @date 2018-11-03 11:24:17
 */
@Service
public class ApiPostsService {
    @Autowired
    private ApiPostsMapper postsDao;


    public PostsVo queryObject(Integer id) {
        return postsDao.queryObject(id);
    }


    public List<PostsVo> queryList(Map<String, Object> map) {
        return postsDao.queryList(map);
    }


    public int queryTotal(Map<String, Object> map) {
        return postsDao.queryTotal(map);
    }


    public int save(PostsVo posts) {
        return postsDao.save(posts);
    }


    public int update(PostsVo posts) {
        return postsDao.update(posts);
    }


    public int delete(Integer id) {
        return postsDao.delete(id);
    }


    public int deleteBatch(Integer[] ids) {
        return postsDao.deleteBatch(ids);
    }

    /**
     * 根据是否点赞增加或减少点赞数
     *
     * @param id
     * @param type 1-增加；0-减少
     * @return
     */
    public void updateLikeCount(Integer id, Integer type) {
        PostsVo postsVo = queryObject(id);
        if (postsVo != null) {
            if (type == 1) {
                postsVo.setLikeCount(postsVo.getLikeCount() + 1);
            } else if (type == 0) {
                if (postsVo.getLikeCount() > 0) {
                    postsVo.setLikeCount(postsVo.getLikeCount() - 1);
                }
            }
        }
        postsDao.update(postsVo);
    }

}
